﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;

namespace AiSk.Sdks
{
    public class AntSkApiService
    {
        public record AntSkRequestData
        {
            public AntSkRequestData(string content)
            {
                messages = [new AntSkRequestMessage(content)];
            }
            public AntSkRequestData(List<AntSkRequestMessage> messageList)
            {
                messages = messageList.ToArray();
            }
            public bool stream { get; set; } = false;

            public AntSkRequestMessage[] messages { get; set; }
        }

        public record AntSkRequestMessage
        {
            public AntSkRequestMessage(string content)
            {
                this.content = content;
            }

            public string role { get; set; } = "user";
            public string content { get; set; } = "";
        }

        public record AntSkResponseMessage
        {
            public string id { get; set; } = null!;

            [JsonPropertyName("object")]
            public string obj { get; set; } = null!;


            public int created { get; set; }

            public AntSkResponseData[] choices { get; set; } = [];

            public string GetResponseMessage()
            {
                var choice = choices.First();
                if (choice?.finish_reason == "stop")
                {
                    var dataStr = choice.message.content;
                    return dataStr;
                }
                return string.Empty;
            }
        }

        public record AntSkResponseData
        {
            public string finish_reason { get; set; } = null!;
            public int index { get; set; }

            public AntSkResponseDataMsg message { get; set; } = null!;
        }

        public record AntSkResponseDataMsg
        {
            public string role { get; set; } = null!;
            public string content { get; set; } = null!;

        }

        public static async Task<object?> ExecuteChat(string url, string token, string inputMessage, string contextMessage, bool isStream = false)
        {
            var client = new HttpClient();
            var contextList = string.IsNullOrWhiteSpace(contextMessage) ?
   new List<AntSkRequestMessage>() :
   JsonSerializer.Deserialize<List<AntSkRequestMessage>>(contextMessage) ?? new List<AntSkRequestMessage>();
            contextList.Add(new AntSkRequestMessage(inputMessage));
            var rqData = new AntSkRequestData(contextList);
            rqData.stream = isStream;
            client.Timeout = TimeSpan.FromSeconds(30);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            try
            {
                if (!isStream)
                {
                    var res = await client.Request<AntSkResponseMessage>(url, "POST", rqData);
                    return res.Result;
                }
                else
                {
                    var res = await client.RequestStream(url, "POST", rqData);
                    return res.Result;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"AntSK执行Chat异常 {ex.Message}");
                return null;
            }
        }
    }
}

